# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libtommath
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.0
pkgrel=4
pkgdesc="Highly optimized and portable routines for integer based number theoretic applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.libtom.net/LibTomMath/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
options=('strip')
source=("https://github.com/libtom/libtommath/releases/download/v${pkgver}/ltm-${pkgver}.tar.xz")
sha256sums=('b7c75eecf680219484055fcedd686064409254ae44bc31a96c5032843c0e18b1')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # Disable unnecessary warnings
  sed -e "s/ -Wsystem-headers//g" -e "s/ -Wshadow//g" -i makefile_include.mk

  # Build both shared and static library
  sed -e "s/\$(LIBMAIN_S) test/\$(LIBMAIN_S) \$(LIBMAIN_D) test/g" -e "s/--export-all/--export-all-symbols/g" -i makefile.mingw

  # Disable static libgcc linking
  sed -e "s/ -static-libgcc//g" -i makefile.mingw
}

build() {
  cp -rf "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  make -f makefile.mingw all \
  CC=${CC} PREFIX=${MINGW_PREFIX}

}

check() {
  cd ${srcdir}/build-${MSYSTEM}

  ./test.exe
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  mkdir -p ${pkgdir}${MINGW_PREFIX}/include
  install -Dm755 libtommath.dll "${pkgdir}${MINGW_PREFIX}/bin/libtommath.dll"
  install -Dm644 libtommath.a "${pkgdir}${MINGW_PREFIX}/lib/libtommath.a"
  install -Dm644 libtommath.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libtommath.dll.a"
  install -Dm644 tommath*.h "${pkgdir}${MINGW_PREFIX}/include"
  install -Dm644 doc/bn.pdf "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/bn.pdf"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
